package org.xiaozhou.chatgptcativen.interfaces;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xiaozhou.chatgptcativen.domain.security.service.JwtUtil;

import java.util.HashMap;
import java.util.Map;

@RestController
public class ApiAccessController {
    private Logger logger = LoggerFactory.getLogger(ApiAccessController.class);
    @RequestMapping("/authorize")
    private ResponseEntity<Map<String,String>> authorize(String username, String password){
        Map<String,String> map = new HashMap<>();
        if (!"xiaozhou".equals(username) || !"123456".equals(password)){
            map.put("msg","登录失败");
            return ResponseEntity.ok(map);
        }else {
            map.put("msg","登录成功");
            Map<String, Object> claims = new HashMap<>();
            claims.put("username",username);
            JwtUtil jwtUtil = new JwtUtil();
            String jwtToken = jwtUtil.encode(username, 5 * 60 * 1000, claims);
            map.put("token",jwtToken);
            return ResponseEntity.ok(map);
        }
    }

    @RequestMapping("/verify")
    private ResponseEntity<String> verify(String token){
        logger.info("验证token：{}",token);
        if ("success".equals(token)){
            return ResponseEntity.status(HttpStatus.OK).build();
        }else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
    }

    @RequestMapping("/success")
    private String success(){
        return "success";
    }
}
